<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopCustomer {

	var $db;
 	var $smarty;	
	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
		
	public function __destruct() {
		//
	}
	
	public function show_customer_list(){
		$customers = array();
		$countcustomer = BWebshopCustomerBean::count_all_customer($this->db);
		$customers['data'] = BWebshopCustomerBean::get_all_customer_paging($this->db,ADMINPAGING,"list",$_GET['sort'],$_GET['by']);
		$result = BgcmsPaging::do_paging($countcustomer,$customers, ADMINPAGING, "?module=BgcmsWebshop&sub1=customer","list");
		$this->smarty->assign("CUSTOMERS", $result);
	}

        public function process(){
  		if(isset($_POST['act'])) {
			$action = $_POST['act'];
			if($action == "newcustomer") {
				$this->create_customer();
			} else if ($action == "editcustomer") {
				$this->edit_customer();
			} else if($action == "delcustomer") {
				$this->delete_customer();
			}else if(isset($_POST['act']) && $_POST['act']=='checkerror'){
				$this->checkerror();
			}
  		}
		if(isset($_GET['sub1']))
			$sub1 = $_GET['sub1'];
		if($sub1 == "")
			$sub1 = "customer";	
  		if(isset($_GET['act']))
			$act = $_GET['act'];
		if($sub1 == "customer") {
			$this->process_customer($sub1);
		}
		else if ($sub1 == "editcustomer") {
			$this->process_customer($sub1);
		} elseif ($sub1 == "CustomerBilling") {
			$this->process_customer_billing($sub1);
		}
		$this->show_customer_list();
	}
	
	public function checkerror() {
		$ret = $this->validate_customer();
		$type = array('ID' => -1, 'message' => $ret);
		echo array2json($type);	
	}
	
	public function process_customer($sub1) {
		$this->smarty->assign("ALLCOUNTRY", BWebshopCountryBean::get_all_country($this->db));
		if(isset($_GET['mess'])) {
			$msg = "";
			switch($_GET['mess']) {
				case 1:
					$msg = _("Customer updated.");
					break;
				case 2:
					$msg = _("Customer not updated. Maybe you have not change anything.");
					break;
				case 3:
					$msg = _("Customer has been added");
					break;		
			}	
			$this->smarty->assign("resp_message", $msg);
		}
		if($_GET['act'] == "editcustomer") {
			$id = $_GET['id'];
			//$country = BWebshopCountryBean::get_all_country($this->db);
			$customer = BWebshopCustomerBean::get_customer_by_id($this->db, $id);
				if($customer->customer_gender != 'm'){
					$customer->female = "checked";
					$customer->male = "";
				}	
				else{
					$customer->male = "checked";
					$customer->female = "";
				}
				if($customer->customer_active == '1'){
					$customer->check_active = "checked";
				}		
				if($customer->customer_newsletter == '1'){
					$customer->check_newsletter = "checked";
				}	
			$this->smarty->assign("CUSTOMER", $customer);
			//$this->smarty->assign("ALLCOUNTRY", $country);
		}
	}
	
	public function create_customer() {
		$msg = BWebshopCustomer::validate_customer();
		$arr = array("code" => -1, "message" => _("Could not add Customer."));
		if($msg['dem'] == 0) {		
			$id = BWebshopCustomerBean::save_customer($this->db, $_POST);
			if($id >=0 ) {
				$arr = array("code" => 0, "message" => _("New Customer has been added."), "customer_id" => $id);
				$message = "New Customer has been added.";
				setcookie("message", $message,time()+5);
			}
		} else {
			$arr = array("code" => -1, "message" => $msg);
		}
		echo array2json($arr);
				
	}
	
	public function edit_customer() {
		if(isset( $_POST['customer_id'])) {
			$msgId = 2;
			$type = array();
			$customer_gender				= "";
			$customer_firstname 			= "";
			$customer_lastname 				= "";
			$customer_dob 					= "";
			$customer_email 				= "";
			$customer_telephone			 	= "";
			$customer_cellphone			 	= "";
			$customer_fax				 	= "";
			$customer_password			 	= BWebshopCustomerBean::getCustomerPassword($this->db, $_POST['customer_id']);
			$customer_newsletter		 	= "";
			$customer_street_address		 	= "";
			$customer_postcode				 	= "";
			$customer_city					 	= "";
			$customer_state				 	= "";
			$customer_country_id		 		= "";
			$customer_company				 	= "";

			if(isset($_POST['customer_gender']))
				$customer_gender = $_POST['customer_gender'];
			if(isset($_POST['customer_firstname']))
				$customer_firstname = $_POST['customer_firstname'];
			if(isset($_POST['customer_lastname']))
				$customer_lastname = $_POST['customer_lastname'];
			if(isset($_POST['customer_dob']))
				$customer_dob = date('Y-m-d',strtotime($_POST['customer_dob']));
			if(isset($_POST['customer_email']))
				$customer_email = $_POST['customer_email'];
			if(isset($_POST['customer_telephone']))
				$customer_telephone = $_POST['customer_telephone'];
			if(isset($_POST['customer_cellphone']))
				$customer_cellphone = $_POST['customer_cellphone'];	
			if(isset($_POST['customer_fax']))
				$customer_fax = $_POST['customer_fax'];			
			if(isset($_POST['customer_password']) && $_POST['customer_password'] != "" && crypt($_POST['customer_password'], '$ntp$') != $customer_password)
				$customer_password = crypt($_POST['customer_password'], '$ntp$');
			if(isset($_POST['customer_newsletter']))
				$customer_newsletter = $_POST['customer_newsletter'];
			
			if(isset($_POST['delivery_gender']))
				$delivery_gender = $_POST['delivery_gender'];	
			if(isset($_POST['delivery_firstname']))
				$delivery_firstname = $_POST['delivery_firstname'];
			if(isset($_POST['delivery_lastname']))
				$delivery_lastname = $_POST['delivery_lastname'];	
			if(isset($_POST['delivery_street_address']))
				$delivery_street_address = $_POST['delivery_street_address'];
			if(isset($_POST['delivery_postcode']))
				$delivery_postcode = $_POST['delivery_postcode'];
			if(isset($_POST['delivery_city']))
				$delivery_city = $_POST['delivery_city'];
			if(isset($_POST['delivery_state']))
				$delivery_state = $_POST['delivery_state'];
			if(isset($_POST['delivery_country_id']))
				$delivery_country_id = $_POST['delivery_country_id'];
				
			if(isset($_POST['billing_gender']))
				$billing_gender = $_POST['billing_gender'];	
			if(isset($_POST['billing_firstname']))
				$billing_firstname = $_POST['billing_firstname'];
			if(isset($_POST['billing_lastname']))
				$billing_lastname = $_POST['billing_lastname'];	
			if(isset($_POST['billing_street_address']))
				$billing_street_address = $_POST['billing_street_address'];
			if(isset($_POST['billing_postcode']))
				$billing_postcode = $_POST['billing_postcode'];
			if(isset($_POST['billing_city']))
				$billing_city = $_POST['billing_city'];
			if(isset($_POST['billing_state']))
				$billing_state = $_POST['billing_state'];
			if(isset($_POST['billing_country_id']))
				$billing_country_id = $_POST['billing_country_id'];	
			if(isset($_POST['customer_company']))
				$customer_company = $_POST['customer_company'];
			if(isset($_POST['customer_active']))
				$customer_active = $_POST['customer_active'];	
															
			
			$cust = array('customer_gender' => $customer_gender,
					  	  'customer_firstname' => $customer_firstname,
						  'customer_lastname' => $customer_lastname,
						  'customer_dob' => $customer_dob,
						  'customer_email' => $customer_email,
						  'customer_telephone' => $customer_telephone,
						  'customer_cellphone' => $customer_cellphone,
						  'customer_fax' => $customer_fax,
						  'customer_password' => $customer_password,
						  'customer_newsletter' => $customer_newsletter,
						  'delivery_gender' => $delivery_gender,
						  'delivery_firstname' => $delivery_firstname,
						  'delivery_lastname' => $delivery_lastname,
						  'delivery_street_address' => $delivery_street_address,
						  'delivery_postcode' => $delivery_postcode,
						  'delivery_city' => $delivery_city,
						  'delivery_state' => $delivery_state,
					 	  'delivery_country_id' => $delivery_country_id,
						  'billing_gender' => $billing_gender,
					 	  'billing_firstname' => $billing_firstname,
						  'billing_lastname' => $billing_lastname,
						  'billing_street_address' => $billing_street_address,
						  'billing_postcode' => $billing_postcode,
						  'billing_city' => $billing_city,
						  'billing_state' => $billing_state,
					 	  'billing_country_id' => $billing_country_id,
					  	  'customer_company' => $customer_company,
						  'customer_active' => $customer_active,
					  	  'customer_id' => $_POST['customer_id']);
			$msg = $this->validate_customer($this->db);
			if($msg['dem'] == 0) {	
				$res = BWebshopCustomerBean::update_customer($this->db,$cust);
				$msgId = 1;
				$arr = array('code' => 0,'msg' => 1);
				$message = "New Customer has been updated.";
				setcookie("message", $message,time()+5);
			} else {
				$arr = array('code' => -1, 'message' => $msg);
			}
			echo array2json($arr);
		}
	}
	
	public function delete_customer() {
		$arr = array('code' => 0, 'message' => "");
		$id = $_POST['id'];
		$res = BWebshopCustomerBean::delete_customer_by_id($this->db, $id);
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = "Customer <u>".$id."</u> has been deleted.";
		} else {
			$arr['code'] = -1;
			$arr['message'] = "Could not delete Customer <u>".$id."</u>.";
		}
		echo array2json($arr);
	}

	public function validate_customer()
    {
    	$msg = array();
		$msg["customer_gender"] = '';
		/*$msg["customer_firstname"] = '';
		$msg["customer_lastname"] = '';*/
		//$msg["customer_dob"] = '';
		$msg["billing_gender"] = '';
		$msg["billing_firstname"] = '';
		$msg["billing_lastname"] = '';
		$msg["billing_street_address"] = '';
		$msg["billing_postcode"] = '';
		$msg["billing_city"] = '';
		$msg["billing_state"] = '';
		$msg["billing_country_id"] = '';
		$msg["delivery_gender"] = '';
		$msg["delivery_firstname"] = '';
		$msg["delivery_lastname"] = '';
		$msg["delivery_street_address"] = '';
		$msg["delivery_postcode"] = '';
		$msg["delivery_city"] = '';
		$msg["delivery_state"] = '';
		$msg["delivery_country_id"] = '';
		$msg["customer_telephone"] = '';
		$msg["customer_fax"] = '';
		$msg["customer_email"] = '';
		$msg["customer_password"] = '';
		$msg["customer_confirmpassword"] = '';
		$dem =0;
		if(isset($_POST['customer_gender']) && $_POST['customer_gender'] == ""){
			$msg["customer_gender"] = FgcmsMultiLanguageBean::changelang('ERR_GENDER');//"Please select your Gender.";
			$dem++;
		}
		/*if(isset($_POST['customer_firstname']) && $_POST['customer_firstname'] == ""){
			$msg["customer_firstname"] = FgcmsMultiLanguageBean::changelang('ERR_FIRSTNAME');//"Your first name could not be empty.";
			$dem++;
		}
		if(isset($_POST['customer_lastname']) && $_POST['customer_lastname'] == ""){
			$msg["customer_lastname"]= FgcmsMultiLanguageBean::changelang('ERR_LASTNAME');//"Your last name could not be empty.";
			$dem++;
		}
		
		if($_POST['customer_dob'] == ""){
			$msg["customer_dob"]= "Your date of birth could not be empty.";
			$dem++;
		}else{
			$flag = $this->checkValidDate(date('Y-m-d',strtotime($_POST['customer_dob'])));
			if($flag == false){
				$msg["customer_dob"]= "Invalid date of birth.";
				$dem++;
			}else{
				$yob = date('Y-m-d',strtotime($_POST['customer_dob'])) + 0;
				$currentyear = date("Y-m-d") + 0;
				if($yob > $currentyear - 10){
					$msg["customer_dob"]= "You must be enough ten years old.";
					$dem++;
				}
			}
		}*/
		if(isset($_POST['billing_gender']) && $_POST['billing_gender'] == ""){
			$msg["billing_gender"] = FgcmsMultiLanguageBean::changelang('ERR_GENDER');//"Please select your Gender.";
			$dem++;
		}
		if(isset($_POST['billing_firstname']) && $_POST['billing_firstname'] == ""){
			$msg["billing_firstname"] = FgcmsMultiLanguageBean::changelang('ERR_FIRSTNAME');//"Your first name could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_lastname']) && $_POST['billing_lastname'] == ""){
			$msg["billing_lastname"]= FgcmsMultiLanguageBean::changelang('ERR_LASTNAME');//"Your last name could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_street_address']) && $_POST['billing_street_address'] == ""){
			$msg["billing_street_address"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_ADDRESS');//"Your street address could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_postcode']) && $_POST['billing_postcode'] == ""){
			$msg["billing_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_POSTCODE');//"Your postcode could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_postcode']) && $_POST['billing_postcode'] != "" && (!is_numeric($_POST['billing_postcode']) || strlen($_POST['billing_postcode']) < 4)){
			$msg["billing_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_POSTCODE_INVALID');//"Invalid postcode.";
			$dem++;
		}
		if(isset($_POST['billing_city']) && $_POST['billing_city'] == ""){
			$msg["billing_city"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_CITY');//"Your city could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_state']) && $_POST['billing_state'] == ""){
			$msg["billing_state"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_STATE');//"Your state/province could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_country_id']) && $_POST['billing_country_id'] == ""){
			$msg["billing_country_id"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_COUNTRY');//"Please select your country.";
			$dem++;
		}
		
		if(isset($_POST['delivery_gender']) && $_POST['delivery_gender'] == ""){
			$msg["delivery_gender"] = FgcmsMultiLanguageBean::changelang('ERR_GENDER');//"Please select your Gender.";
			$dem++;
		}
		if(isset($_POST['delivery_firstname']) && $_POST['delivery_firstname'] == ""){
			$msg["delivery_firstname"] = FgcmsMultiLanguageBean::changelang('ERR_FIRSTNAME');//"Your first name could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_lastname']) && $_POST['delivery_lastname'] == ""){
			$msg["delivery_lastname"]= FgcmsMultiLanguageBean::changelang('ERR_LASTNAME');//"Your last name could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_street_address']) && $_POST['delivery_street_address'] == ""){
			$msg["delivery_street_address"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_ADDRESS');//"Your street address could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_postcode']) && $_POST['delivery_postcode'] == ""){
			$msg["delivery_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_POSTCODE');//"Your postcode could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_postcode']) && $_POST['delivery_postcode'] != "" && (!is_numeric($_POST['delivery_postcode']) || strlen($_POST['delivery_postcode']) < 4)){
			$msg["delivery_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_POSTCODE_INVALID');//"Invalid postcode.";
			$dem++;
		}
		if(isset($_POST['delivery_city']) && $_POST['delivery_city'] == ""){
			$msg["delivery_city"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_CITY');//"Your city could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_state']) && $_POST['delivery_state'] == ""){
			$msg["delivery_state"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_STATE');//"Your state/province could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_country_id']) && $_POST['delivery_country_id'] == ""){
			$msg["delivery_country_id"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_COUNTRY');//"Please select your country.";
			$dem++;
		}
		if(isset($_POST['customer_telephone']) && $_POST['customer_telephone'] == ""){
			$msg["customer_telephone"]= FgcmsMultiLanguageBean::changelang('ERR_TELEPHONE');//"Your telephone number could not be empty.";
			$dem++;
		}
		if(isset($_POST['customer_telephone']) && $_POST['customer_telephone'] != "" && (strlen($_POST['customer_telephone']) < 6 || (!preg_match('/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/', $_POST['customer_telephone'])))){
			$msg["customer_telephone"]= FgcmsMultiLanguageBean::changelang('ERR_TELEPHONE_INVALID');//"Invalid telephone number.";
			$dem++;
		}
		if(isset($_POST['customer_fax']) && $_POST['customer_fax'] != "" && !is_numeric($_POST['customer_fax'])){
			$msg["customer_fax"]= FgcmsMultiLanguageBean::changelang('ERR_FAX');//"Invalid fax number.";
			$dem++;
		}
		if(isset($_POST['customer_email']) && $_POST['customer_email'] == ""){
			$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL');//"Your email address could not be empty.";
			$dem++;
		}
		if(isset($_POST['customer_email']) && $_POST['customer_email'] != ""){
			$flag = $this->check_email_address($_POST['customer_email']);
			if($flag == false){
				$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL_INVALID');//"Invalid email address.";
				$dem++;
			}else{
				if(isset($_POST['customer_id'])){
					$customer_email = BWebshopCustomerBean::getCustomerEmail($this->db, $_POST['customer_id']);
					if($_POST['customer_email'] != $customer_email)
					{
						$email = BWebshopCustomerBean::getCustomerByEmail($this->db,$_POST['customer_email'],$customer_email);
						if($email!=""){
									$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL_EXISTS');//"This email address already exists.";
									$dem++;
						}
					}
				}else{
						$email = BWebshopCustomerBean::getCustomerByEmail($this->db,$_POST['customer_email'],'');
						if($email!=""){
							$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL_EXISTS');//"This email address already exists.";
							$dem++;
						}
				}
			}
			
		}
		
		if(isset($_POST['customer_id']))
		{
			if(isset($_POST['customer_password']) && $_POST['customer_password'] != "")
			{
				if(strlen($_POST['customer_password']) <5){
					$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_ATLEAST');//"Your password must contain at least 5 characters.";
					$dem++;
				}
				$customer_password = BWebshopCustomerBean::getCustomerPassword($this->db, $_POST['customer_id']);
				if(crypt($_POST['customer_password'], '$ntp$') != $customer_password){
					if($_POST['customer_confirmpassword'] == ""){
						$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD');//"Your password confirmation could not be empty.";
						$dem++;
					}
					if($_POST['customer_confirmpassword'] != "" && $_POST['customer_confirmpassword'] != $_POST['customer_password']){
						$msg["customer_confirmpassword"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_CONFIRM_NOTMATCH');//"Your password confirmation must match your password.";	
						$dem++;
					}
				}
			}
		}
		else
		{
				if($_POST['customer_password'] == ""){
					$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD');//"Your password could not be empty.";
					$dem++;
				}
				if($_POST['customer_password'] != "" && strlen($_POST['customer_password']) <5){
					$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_ATLEAST');//"Your password must contain at least 5 characters.";
					$dem++;
				}	
				if($_POST['customer_confirmpassword'] == ""){
					$msg["customer_confirmpassword"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_CONFIRM');//"Your password confirmation could not be empty.";
					$dem++;
				}
				if($_POST['customer_confirmpassword'] != "" && $_POST['customer_confirmpassword'] != $_POST['customer_password']){
					$msg["customer_confirmpassword"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_CONFIRM_NOTMATCH');//"Your password confirmation must match your password.";	
					$dem++;
				}
		}
		$msg["dem"] = $dem;
		return $msg;
    }
    //@TODO in LIBS
    function check_email_address($email) {
    	// First, we check that there's one @ symbol, and that the lengths are right
    	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
			// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
			return false;
    	}
    	// Split it into sections to make life easier
    	$email_array = explode("@", $email);
    	$local_array = explode(".", $email_array[0]);
   		for ($i = 0; $i < sizeof($local_array); $i++) {
   			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
				return false;
   			}
   		}
   		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {  // Check if domain is IP. If not, it should be valid domain name
   			$domain_array = explode(".", $email_array[1]);
   			if (sizeof($domain_array) < 2) {
				return false; // Not enough parts to domain
   			}
   			for ($i = 0; $i < sizeof($domain_array); $i++) {
   				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
					return false;
   				}
   			}
   		}
   		return true;
	}
    
}
?>